﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileSysManager.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the FsManager type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Directory2Rss.Data
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    /// <summary>
    /// This class handles all the FileSystem interactions needed
    /// </summary>
    public class FileSysManager
    {
        /// <summary>
        /// Queries the file system 
        /// </summary>
        /// <param name="scanFolder">
        /// The scan Folder.
        /// </param>
        /// <returns>
        /// a List containing all the files within the scanned folders
        /// </returns>
        public List<Types.Files> GetFsFiles(string scanFolder)
        {
            // Initialize the fileSysList
            List<Types.Files> fileSysList = (from file in GetDirectoryContents(scanFolder).AsQueryable()
                                             select new Types.Files
                                             {
                                                 FilePath = file.DirectoryName,
                                                 FileName = file.Name
                                             }).ToList();
            return fileSysList;
        }

        /// <summary>
        /// This writes the XML file to disk
        /// </summary>
        /// <param name="outputFile">
        /// The output file.
        /// </param>
        /// <param name="xmlResults">
        /// The xml results.
        /// </param>
        public void WriteRssXmlFile(string outputFile, string xmlResults)
        {
            try
            {
                File.WriteAllText(outputFile, xmlResults);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error writing RSS XML\n  : {0}", e);
                throw;
            }
        }

        /// <summary>
        /// This method assumes that the application has discovery 
        /// permissions for all folders under the specified path.
        /// </summary>
        /// <param name="path">
        /// The path to search and return from
        /// </param>
        /// <returns>
        /// A list of files contained within the path
        /// </returns>
        /// <exception cref="DirectoryNotFoundException">
        /// </exception>
        private static List<FileInfo> GetDirectoryContents(string path)
        {
            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException();
            }

            List<FileInfo> files = new List<FileInfo>();

            string[] fileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            foreach (string name in fileNames)
            {
                files.Add(new FileInfo(name));
            }

            return files;
        }
    }
}
